/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * CategoriesNDPanel.java
 *
 * Created on 20/11/2009, 11:39:35
 */

package desktopapplication3;

import backend.Catalog;
import backend.Category;
import java.awt.BorderLayout;
import java.util.Map;
import javax.swing.JOptionPane;

/**
 *
 * @author santiago
 */
public class CategoriesNDPanel extends javax.swing.JPanel {

    private static final long serialVersionUID = 1L;
    private Map<String,Category> myCat;
    private int languageCode;
    org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(desktopapplication3.DesktopApplication3.class).getContext().getResourceMap(CategoriesNDPanel.class);
    /** Creates new form CategoriesNuevoDisenioFrame */

    /** Creates new form CategoriesNDPanel */
    public CategoriesNDPanel(int languageCode) {
        this.languageCode=languageCode;
        try{
            myCat= Catalog.getCategoryList( languageCode );

        }catch(Exception a){
                org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(desktopapplication3.DesktopApplication3.class).getContext().getResourceMap(VentanaPrincipal.class);
                JOptionPane.showMessageDialog(this,resourceMap.getString("ERROR_"+a.getMessage()),"Error",1);
                System.exit(0);
         }
        initMyComponents();
    }


    private void initMyComponents(){

        productPanel = new javax.swing.JPanel();
        listPanel = new javax.swing.JPanel();
        categoriesCombo = new javax.swing.JComboBox();
        categoriesTitle = new javax.swing.JLabel();
        criteriaSearch = new javax.swing.JTextField();
        searchLa = new javax.swing.JLabel();
        //BuscarButton = new javax.swing.JButton();

        setName("Form"); // NOI18N

        productPanel.setName("productPanel"); // NOI18N

        javax.swing.GroupLayout productPanelLayout = new javax.swing.GroupLayout(productPanel);
        productPanel.setLayout(productPanelLayout);
        productPanelLayout.setHorizontalGroup(
            productPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 472, Short.MAX_VALUE)
        );
        productPanelLayout.setVerticalGroup(
            productPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 318, Short.MAX_VALUE)
        );

        listPanel.setName("listPanel"); // NOI18N

        javax.swing.GroupLayout listPanelLayout = new javax.swing.GroupLayout(listPanel);
        listPanel.setLayout(listPanelLayout);
        listPanelLayout.setHorizontalGroup(
            listPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 136, Short.MAX_VALUE)
        );
        listPanelLayout.setVerticalGroup(
            listPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 318, Short.MAX_VALUE)
        );
        Object[] myVecString = myCat.keySet().toArray();
        categoriesCombo.setModel(new javax.swing.DefaultComboBoxModel(myVecString));
        categoriesCombo.setName("categoriesCombo"); // NOI18N
        categoriesCombo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                categoriesComboActionPerformed(evt);
            }
        });

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(desktopapplication3.DesktopApplication3.class).getContext().getResourceMap(CategoriesNDPanel.class);
        categoriesTitle.setText(resourceMap.getString("categoriesTitle.text")); // NOI18N
        categoriesTitle.setName("categoriesTitle"); // NOI18N


        criteriaSearch.setToolTipText(resourceMap.getString("toolTipText"));

        criteriaSearch.setName("criteriaSearch"); // NOI18N
        // tooltip
        categoriesTitle.setFont(resourceMap.getFont("categoriesTitle.font")); // NOI18N
        categoriesTitle.setText(resourceMap.getString("categoriesTitle.text")); // NOI18N
        categoriesTitle.setName("categoriesTitle"); // NOI18N
        criteriaSearch.addKeyListener(new java.awt.event.KeyAdapter() {
            @Override
            public void keyReleased(java.awt.event.KeyEvent evt) {
                criteriaSearchKeyTyped(evt);
            }
        });
        searchLa.setFont(resourceMap.getFont("searchLa.font")); // NOI18N
        searchLa.setText(resourceMap.getString("searchLa.text")); // NOI18N
        searchLa.setName("searchLa"); // NOI18N

        //criteriaSearch.setText(resourceMap.getString("criteriaSearch.text")); // NOI18N
        //criteriaSearch.setName("criteriaSearch"); // NOI18N

     /*   BuscarButton.setText(resourceMap.getString("BuscarButton.text")); // NOI18N
        BuscarButton.setName("BuscarButton"); // NOI18N
        BuscarButton.addMouseListener(new java.awt.event.MouseAdapter() {
            @Override
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                BuscarButtonMouseClicked(evt);
            }
        });*/

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 644, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(categoriesCombo, javax.swing.GroupLayout.PREFERRED_SIZE, 147, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(140, 140, 140))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(categoriesTitle, javax.swing.GroupLayout.DEFAULT_SIZE, 370, Short.MAX_VALUE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
                        .addGap(12, 12, 12)
                        .addComponent(searchLa)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(criteriaSearch, javax.swing.GroupLayout.PREFERRED_SIZE, 171, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(listPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(productPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 410, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(categoriesTitle)
                    .addComponent(criteriaSearch, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(searchLa))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(categoriesCombo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(productPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(listPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
         ListaCatPanel aCargar = new ListaCatPanel(myCat.get((String)(myCat.keySet().toArray()[0])),productPanel,languageCode);
        aCargar.setVisible(true);
        listPanel.removeAll();
        listPanel.setLayout(new BorderLayout());
        listPanel.add(aCargar,BorderLayout.CENTER);
        aCargar.updateUI();
        listPanel.updateUI();
}



    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        productPanel = new javax.swing.JPanel();
        listPanel = new javax.swing.JPanel();
        categoriesCombo = new javax.swing.JComboBox();
        categoriesTitle = new javax.swing.JLabel();
        criteriaSearch = new javax.swing.JTextField();
        searchLa = new javax.swing.JLabel();

        setName("Form"); // NOI18N

        productPanel.setName("productPanel"); // NOI18N

        javax.swing.GroupLayout productPanelLayout = new javax.swing.GroupLayout(productPanel);
        productPanel.setLayout(productPanelLayout);
        productPanelLayout.setHorizontalGroup(
            productPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 472, Short.MAX_VALUE)
        );
        productPanelLayout.setVerticalGroup(
            productPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 320, Short.MAX_VALUE)
        );

        listPanel.setName("listPanel"); // NOI18N

        javax.swing.GroupLayout listPanelLayout = new javax.swing.GroupLayout(listPanel);
        listPanel.setLayout(listPanelLayout);
        listPanelLayout.setHorizontalGroup(
            listPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 136, Short.MAX_VALUE)
        );
        listPanelLayout.setVerticalGroup(
            listPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 320, Short.MAX_VALUE)
        );

        categoriesCombo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        categoriesCombo.setName("categoriesCombo"); // NOI18N
        categoriesCombo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                categoriesComboActionPerformed(evt);
            }
        });

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(desktopapplication3.DesktopApplication3.class).getContext().getResourceMap(CategoriesNDPanel.class);
        categoriesTitle.setFont(resourceMap.getFont("categoriesTitle.font")); // NOI18N
        categoriesTitle.setText(resourceMap.getString("categoriesTitle.text")); // NOI18N
        categoriesTitle.setName("categoriesTitle"); // NOI18N

        criteriaSearch.setText(resourceMap.getString("criteriaSearch.text")); // NOI18N
        criteriaSearch.setName("criteriaSearch"); // NOI18N
        criteriaSearch.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                criteriaSearchKeyTyped(evt);
            }
        });

        searchLa.setFont(resourceMap.getFont("searchLa.font")); // NOI18N
        searchLa.setText(resourceMap.getString("searchLa.text")); // NOI18N
        searchLa.setName("searchLa"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 644, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(categoriesCombo, javax.swing.GroupLayout.PREFERRED_SIZE, 147, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(140, 140, 140))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(categoriesTitle, javax.swing.GroupLayout.DEFAULT_SIZE, 370, Short.MAX_VALUE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
                        .addGap(12, 12, 12)
                        .addComponent(searchLa)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(criteriaSearch, javax.swing.GroupLayout.PREFERRED_SIZE, 171, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(listPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(productPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 410, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(categoriesTitle)
                    .addComponent(criteriaSearch, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(searchLa))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(categoriesCombo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(productPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(listPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void categoriesComboActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_categoriesComboActionPerformed
        if(evt.getActionCommand().equals("comboBoxChanged")){
            
            ListaCatPanel aCargar = new ListaCatPanel(myCat.get((String)categoriesCombo.getSelectedItem()),productPanel,languageCode);
            aCargar.setVisible(true);
            listPanel.removeAll();
            listPanel.setLayout(new BorderLayout());
            listPanel.add(aCargar,BorderLayout.CENTER);
            aCargar.updateUI();
            listPanel.updateUI();
        }
}//GEN-LAST:event_categoriesComboActionPerformed

    private void criteriaSearchKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_criteriaSearchKeyTyped
        
        if(!criteriaSearch.getText().equals("")){
            ProductsPanel aCargar = new ProductsPanel(criteriaSearch.getText());
            aCargar.setVisible(true);
            productPanel.removeAll();
            productPanel.setLayout(new BorderLayout());
            productPanel.add(aCargar,BorderLayout.CENTER);
            aCargar.updateUI();
            productPanel.updateUI();
        }else{
            categoriesCombo.setSelectedIndex(0);
            ListaCatPanel aCargar = new ListaCatPanel(myCat.get((String)(myCat.keySet().toArray()[0])),productPanel,languageCode);
            aCargar.setVisible(true);
            listPanel.removeAll();
            listPanel.setLayout(new BorderLayout());
            listPanel.add(aCargar,BorderLayout.CENTER);
            aCargar.updateUI();
            listPanel.updateUI();
        }
    }//GEN-LAST:event_criteriaSearchKeyTyped


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox categoriesCombo;
    private javax.swing.JLabel categoriesTitle;
    private javax.swing.JTextField criteriaSearch;
    private javax.swing.JPanel listPanel;
    private javax.swing.JPanel productPanel;
    private javax.swing.JLabel searchLa;
    // End of variables declaration//GEN-END:variables

}
